package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    private UserInfoService userInfoService;

    // 用户列表
    public ResponseEntity users(Integer pageNum, Integer pageSize) {
        //1. 调用rpc查询
        PageBeanVo pageBeanVo = userInfoService.findUserInfoByPage(pageNum, pageSize);
        //2. 返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 用户详情
    public ResponseEntity findUserInfoById(Long userId) {
      //1. 调用rpc查询
        UserInfo userInfo = userInfoService.findById(userId);
        //2. 返回userInfo
        return ResponseEntity.ok(userInfo);
    }

    @DubboReference
    private MovementService movementService;

    // 动态查询
    public ResponseEntity messages(Long userId, Integer stateInt, Integer pageNum, Integer pageSize) {
       //1. 调用rpc查询动态分页
        PageBeanVo pageBeanVo = movementService.findMovementByPage(userId, stateInt, pageNum, pageSize);
        //2. 封装vo
        //2-1 声明voList
        ArrayList<MovementVo> voList = new ArrayList<>();
        //2-2 获取movementList
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        //2-3 遍历movementList
        if(CollectionUtil.isNotEmpty(movementList)){
            for (Movement movement : movementList) {
                //创建vo
                MovementVo vo = new MovementVo();
                //先userInfo
                vo.setUserInfo(userInfoService.findById(movement.getUserId()));
                // 再movement
                vo.setMovement(movement);

                //设置具体动态时间
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
                //添加vo集合
                voList.add(vo);
            }
        }
        //3.将voList设置到分页对象
        pageBeanVo.setItems(voList);
        //4. 返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 动态详情
    public ResponseEntity findMovementVoById(String publishId) {
       //1. 调用rpc查询
        Movement movement = movementService.findById(new ObjectId(publishId));
        //2. 封装vo
        //创建vo
        MovementVo vo = new MovementVo();
        //先userInfo
        vo.setUserInfo(userInfoService.findById(movement.getUserId()));
        //再movement
        vo.setMovement(movement);
        vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
        //3.返回vo
        return ResponseEntity.ok(vo);

    }

    @DubboReference
    private CommentService commentService;

    // 评论列表
    public ResponseEntity findCommentVoByPage(String publishId, Integer pageNum, Integer pageSize) {
        //1. 调用rpc查询
        PageBeanVo pageBeanVo = commentService.findCommentByPage(new ObjectId(publishId), 2, pageNum, pageSize);
        //2.封装commentVo
        //2-1 声明voList
        ArrayList<CommentVo> voList = new ArrayList<>();
        //2-2 获取commentList
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        //2-3 遍历
        if(CollectionUtil.isNotEmpty(commentList)){
            for (Comment comment : commentList) {
                //创建vo
                CommentVo vo = new CommentVo();
                //先userInfo
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                //后comment
                vo.setId(comment.getId().toHexString());
                vo.setContent(comment.getContent());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 2023-03-18 11:10

                //添加到集合
                voList.add(vo);
            }
        }
        //3. 将voList设置到分页对象
        pageBeanVo.setItems(voList);
        //4. 返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }
}
